﻿using System;

// 定义链表节点类
class Node<T>
{
    public T Data { get; set; }
    public Node<T> Next { get; set; }

    public Node(T data)
    {
        Data = data;
        Next = null;
    }
}

// 定义泛型链表类
class GenericLinkedList<T>
{
    private Node<T> head;

    public void Add(T data)
    {
        Node<T> newNode = new Node<T>(data);

        if (head == null)
        {
            head = newNode;
        }
        else
        {
            Node<T> current = head;
            while (current.Next != null)
            {
                current = current.Next;
            }
            current.Next = newNode;
        }
    }

    public void ForEach(Action<T> action)
    {
        Node<T> current = head;
        while (current != null)
        {
            action(current.Data);
            current = current.Next;
        }
    }
}

class Program
{
    static void Main()
    {
        GenericLinkedList<int> linkedList = new GenericLinkedList<int>();

        linkedList.Add(10);
        linkedList.Add(20);
        linkedList.Add(30);
        linkedList.Add(40);
        linkedList.Add(50);

        Console.WriteLine("链表元素：");
        linkedList.ForEach(i => Console.WriteLine(i));

        int sum = 0;
        int max = int.MinValue;
        int min = int.MaxValue;

        linkedList.ForEach(i =>
        {
            sum += i;
            max = Math.Max(max, i);
            min = Math.Min(min, i);
        });

        Console.WriteLine("元素和：" + sum);
        Console.WriteLine("最大值：" + max);
        Console.WriteLine("最小值：" + min);

        Console.ReadLine();
    }
}